package com.ckmod.zyr3x.tools.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.ckmod.zyr3x.tools.R;
import com.ckmod.zyr3x.tools.components.CSSConfig;
import com.ckmod.zyr3x.tools.components.ComplexCheckedBilder;
import com.ckmod.zyr3x.tools.components.SeparatedListAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: zyr3x
 * Date: 12.07.11
 * Time: 9:54
 * To change this template use File | Settings | File Templates.
 */
public class Main extends Activity
{
    public final static String ITEM_TITLE = "title";
    public final static String ITEM_CAPTION = "caption";
    public final static String ITEM_VALUE = "value";

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        CSSConfig.init(getApplicationContext().getSharedPreferences("SS4N1",0),getResources());
        CSSConfig.stop();
        CSSConfig.start();
        init();
    }

    public void init()
    {
        SeparatedListAdapter adapter = new SeparatedListAdapter(this);

        adapter.addSection(getString(R.string.ss_title), simpleAdapter(getResources().getStringArray(R.array.switch_caption),
                getResources().getStringArray(R.array.switch_title),
                getResources().getStringArray(R.array.switch_value),
                R.layout.list_complex_checked));

        adapter.addSection(getString(R.string.ss_conf), simpleAdapter(
                getResources().getStringArray(R.array.settings),
                getResources().getStringArray(R.array.settings_info),
                null,
                R.layout.list_complex));

        adapter.addSection(getString(R.string.profiles), simpleAdapter(
                getResources().getStringArray(R.array.profiles),
                getResources().getStringArray(R.array.profiles_info),
                null,
                R.layout.list_complex_menu));

        ListView list = new ListView(this);
        list.setAdapter(adapter);
        list.setOnItemClickListener(onItemClickListener());
        setContentView(list);

    }

    public SimpleAdapter simpleAdapter(String[] names, String[] infos, String[] values,int layout)
    {
        ArrayList<HashMap<String,String>> listData =  new ArrayList<HashMap<String,String>>();

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listData,
                                layout,
                                new String[] {ITEM_TITLE, ITEM_CAPTION},
                                new int[] {R.id.list_complex_title, R.id.list_complex_caption});
        simpleAdapter.setViewBinder(new ComplexCheckedBilder());

        for (int i = 0; i< names.length; i++)
        {
                    HashMap<String,String> temp = new HashMap<String,String>();
                    if(values!=null)
                        temp.put(ITEM_TITLE, names[i]+":"+ values[i]);
                    else
                        temp.put(ITEM_TITLE, names[i]);

                    if(infos !=null)
                        temp.put(ITEM_CAPTION, infos[i]);
                    else
                        temp.put(ITEM_CAPTION, "");
                    listData.add(temp);
        }

        simpleAdapter.notifyDataSetChanged();

        return simpleAdapter;
    }


    private AdapterView.OnItemClickListener onItemClickListener()
    {
      return new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch(i)
                {
                    case 1:
                        CheckedTextView v = (CheckedTextView)view.findViewById(R.id.list_complex_title);
                        boolean current = v.isChecked();
                        v.setChecked ((current == true)? false : true);
                        CSSConfig.setValue(getResources().getStringArray(R.array.switch_value)[0], ((current == true) ? "0" : "1"));
                    break;
                    case 3:
                        FeaturesActivity();
                        break;
                    case 4:
                        ASModActivity();
                        break;
                    case 5:
                        ChargModActivity();
                        break;
                    case 7:
                        saveDialog();
                        break;
                    case 8:
                        loadDialog();
                        break;
                    case 9:
                        deleteDialog();
                    break;
                }

            }
        };
    }

    private void ASModActivity()
    {
          Intent i = new Intent();
          i.setClass(this, ASMode.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(i);
    }

    private void ChargModActivity()
    {
          Intent i = new Intent();
          i.setClass(this, Charging.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(i);
    }

    private void FeaturesActivity()
    {
          Intent i = new Intent();
          i.setClass(this, Features.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(i);
    }

    public void saveDialog()
    {
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        alert.setTitle(getString(R.string.profile_save));
        alert.setView(input);
        input.setInputType(1);

        alert.setPositiveButton(getString(R.string.profile_save), new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      String value = input.getText().toString().trim();
                      if(value != "")
                      {
                         CSSConfig.saveCurrent(value,"");
                         dialog.cancel();
                      }
                      else
                      {
                         Toast.makeText(getApplicationContext(),"Error! Enter name", Toast.LENGTH_SHORT).show();
                      }
                  }
              });

        alert.setNegativeButton(getString(R.string.back),
                  new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                          dialog.cancel();
                      }
                  });
          alert.show();


    }

    public void loadDialog()
    {


      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(getString(R.string.profile_load));
      final String[] nameProfiles = CSSConfig.loadProfilesList();
      builder.setItems(nameProfiles, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                     boolean status = CSSConfig.saveProfilesName(nameProfiles[item]);
                    dialog.dismiss();
                    if(status)
                        loadProfileComplite();
                         else
                        loadProfileError();


            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

    public void loadProfileComplite()
    {
        Toast.makeText(getApplicationContext(),R.string.profile_load_complite, Toast.LENGTH_LONG).show();
    }

    public void loadProfileError()
    {
        Toast.makeText(getApplicationContext(),R.string.profile_load_error,  Toast.LENGTH_LONG).show();;
    }

    public void deleteDialog()
    {

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(getString(R.string.profile_del));
      final String[] nameProfiles = CSSConfig.loadProfilesList();
      builder.setItems(nameProfiles, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                     CSSConfig.deleteProfilesName(nameProfiles[item]);
                     dialog.dismiss();
            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }


}
